/**
 * 体重api
 * @author hejiacheng
 * @date 2021-12-3
 */

import {
	getTableOperate
} from '@/utils/cloudDatabaseUtil.js'
import storageUtil from '../utils/storageUtil'

const tableName = 'user'

/**
 * 添加用户信息
 * @param {时间对象} date 
 */
export function addUserApi(data) {
	return new Promise(function(resolve, reject) {
		getTableOperate(tableName).add({
			env: data.env,
			openId: data.openId
		}).then((res) => {
			resolve && resolve(res)
		}, (res) => {
			reject && reject(res)
		})
	})
}

/**
 * 删除体重
 * @param {时间对象} date 
 */
export function updateUserApi(data) {
	let openId = storageUtil.getOpenId()
	let env = storageUtil.getEnv()
	return new Promise(function(resolve, reject) {
		getTableOperate(tableName)
		.where({
			openId: openId,
			env: env
		}).update({
			nickName: data.nickName,
			avatarUrl: data.avatarUrl
		}).then((res) => {
			resolve && resolve(res)
		}, (res) => {
			reject && reject(res)
		})
	})
}


/**
 * 查询用户
 * @param {时间对象} date 
 */
export function findUserApi(data) {
	console.log('data : ', data)
	return new Promise(function(resolve, reject) {
		getTableOperate(tableName).where({
				env: data.env,
				openId: data.openId
			})
			.limit(10) // 限制返回数量为 10 条
			.get().then((res) => {
				console.log('查询信息 res : ', res)
				if (res.result && res.result.data  && res.result.data.length >= 0) {
					resolve && resolve(res.result.data[0])
				} else {
					resolve && resolve(null)
				}
			}, (res) => {
				reject && reject(res)
			})
	})
}
